package com.android.luhnchecking.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.luhnchecking.LuhnNumber;
import com.android.luhnchecking.R;

public class AccountAdapter extends BaseAdapter {
	private List<LuhnNumber> mNumbers;
	private LayoutInflater mInflater;

	public AccountAdapter(final Context context, final List<LuhnNumber> list) {
		mNumbers = list;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void add(final LuhnNumber number) {
		mNumbers.add(number);
		notifyDataSetChanged();
	}

	public void clear() {
		while (mNumbers.size() > 1) {
			mNumbers.remove(1);
		}
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mNumbers.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mNumbers.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.layout_account_item, null);
			// initialize ViewHolder
			viewHolder = new ViewHolder();
			viewHolder.background = (LinearLayout) convertView
					.findViewById(R.id.ll_background);
			viewHolder.account = (TextView) convertView
					.findViewById(R.id.tv_account);
			viewHolder.status = (TextView) convertView
					.findViewById(R.id.tv_status);
			viewHolder.time = (TextView) convertView.findViewById(R.id.tv_time);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		// push data to view
		if (position != 0) {
			LuhnNumber number = (LuhnNumber) getItem(position);
			if (number.isPassed()) {
				viewHolder.background.setBackgroundColor(Color.GREEN);
			} else {
				viewHolder.background.setBackgroundColor(Color.RED);
			}
			SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm aa");
			viewHolder.account.setText(number.getValue());
			viewHolder.status.setText(String.valueOf(number.isPassed())
					.toUpperCase());
			viewHolder.time.setText(df.format(new Date(number.getTimeStamp())));
		} else {
			viewHolder.background.setBackgroundColor(Color.GRAY);
			viewHolder.account.setText("Account Number");
			viewHolder.status.setText("Result");
			viewHolder.time.setText("Attempt At");
		}
		return convertView;
	}

	public static class ViewHolder {
		public LinearLayout background;
		public TextView account;
		public TextView status;
		public TextView time;
	}
}
